package com.ltxtiyu.video.configuration;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
public class ElasticSearchConfigration {

    @Value("${elasticsearch.inetAddress}")
    private String inetAddress;
    @Value("${elasticsearch.port}")
    private int port;
    /**
     * 获取TransportClient
     * @return
     * @throws UnknownHostException
     */
    @Bean(name = "transportClient")
    public TransportClient transportClients() throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "docker-cluster").build();
        TransportClient client = null;
        synchronized (TransportClient.class){
            try {
                client=new PreBuiltTransportClient(settings)
                        .addTransportAddress(new TransportAddress(InetAddress.getByName(inetAddress), port));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
        return client;
    }
}
